package com.yami.shop.api.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.api.config.DistanceUtils;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.ViewUserRecord;
import com.yami.shop.dao.UnlockRecordMapper;
import com.yami.shop.dao.ViewUserRecordMapper;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.UserService;
import com.yami.shop.service.ViewUserRecordService;
import com.yami.shop.common.util.PageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 浏览用户记录 前端控制器
 * </p>
 *
 * @author chajiu
 * @since 2021-06-30
 */
@RestController
@RequestMapping("/p/view-user")
@Api(tags = "消息接口")
@AllArgsConstructor
public class ViewUserController {


    @Autowired
    private ViewUserRecordService ViewUserRecordService;
    @Autowired
    private UserService userService;
    @Autowired
    private UnlockRecordMapper unlockRecordMapper;
    @Autowired
    private ViewUserRecordMapper viewUserRecordMapper;

    @GetMapping("/page")
    @ApiOperation(value = "获取谁看过你列表", notes = "获取看过你的用户列表")
    public ResponseEntity<List<ViewUserRecord>> page() {
        String userId = SecurityUtils.getUser().getUserId();
        User user = userService.getById(userId);
        List<ViewUserRecord> views = viewUserRecordMapper.queryViewUser(userId);
        for (ViewUserRecord vie : views) {
            User one = userService.getById(vie.getFromUserId());
            vie.setFromUserName(one.getNickName());
            vie.setFromUserPic(one.getPic());
            if(user.getLng() != null && user.getLat() != null && one.getLng() != null && one.getLat() != null){
                Double dist = DistanceUtils.getDistance(user.getLng(),user.getLat(),one.getLng(),one.getLat());
                vie.setDistance(dist);
            }
            Integer sta = unlockRecordMapper.unlUser(userId,one.getUserId());
            if(!sta.equals(0)){
                vie.setUnlStatus(1);
            }else{
                vie.setUnlStatus(2);
            }
        }
        return ResponseEntity.ok(views);
    }


}

